package com.example.lijinyuan.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by lijinyuan on 16/4/26.
 */
public class ListAdapter extends BaseAdapter {

    private static final int TYPE_IMG_RIGHT = 0;
    private static final int TYPE_IMG_LIFT = 1;
    //源码中有这么句话     An integer representing the type of View. Two views should share the same type if one
//    *                 can be converted to the other in {@link #getView}. Note: Integers must be in the
//    *                 range 0 to {@link #getViewTypeCount} - 1. {@link #IGNORE_ITEM_VIEW_TYPE} can
//    *                 also be returned.
    private ViewHolder viewHolder;
    private Context mContext;
    private List<ListBean> mListBeen;

    public ListAdapter(Context mContext, List<ListBean> mListBeen) {
        this.mContext = mContext;

        this.mListBeen = mListBeen;
    }

    @Override
    public int getCount() {
        return mListBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return mListBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position % 2 == 0 ? TYPE_IMG_RIGHT : TYPE_IMG_LIFT;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder = new ViewHolder();
        int type = getItemViewType(position);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (convertView == null) {
            switch (type) {
                case TYPE_IMG_RIGHT:
                    convertView = inflater.inflate(R.layout
                            .img_right, null);
                    viewHolder.textViewRight = (TextView) convertView.findViewById(R.id.right_title);
                    viewHolder.imageViewRight = (ImageView) convertView.findViewById(R.id.right_img);
                    break;
                case TYPE_IMG_LIFT:
                    convertView = inflater.inflate(R.layout
                            .img_left, null);
                    viewHolder.textViewLeft = (TextView) convertView.findViewById(R.id.left_title);
                    viewHolder.imageViewLeft = (ImageView) convertView.findViewById(R.id.left_img);
                    break;
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (type == TYPE_IMG_RIGHT) {
            viewHolder.textViewRight.setTextColor(mContext.getResources().getColor(setColor(position)));
            viewHolder.textViewRight.setText(mListBeen.get(position).getTitle());
            viewHolder.imageViewRight.setImageResource(mListBeen.get(position).getImg());
        }
        if (type == TYPE_IMG_LIFT) {
            viewHolder.textViewLeft.setTextColor(mContext.getResources().getColor(setColor(position)));
            viewHolder.textViewLeft.setText(mListBeen.get(position).getTitle());
            viewHolder.imageViewLeft.setImageResource(mListBeen.get(position).getImg());

        }
        return convertView;
    }

    static class ViewHolder {

        private TextView textViewLeft, textViewRight;
        private ImageView imageViewLeft, imageViewRight;


    }

    private int setColor(int position) {

        switch (position % 3) {
            case 0:
                return R.color.red;

            case 1:
                return R.color.yellow;

            case 2:
                return R.color.green;

        }
        return 0;
    }
}
